package com.frinder.hadoop.job.joiner;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/**
 * @author frinder
 * @date 2018/6/26
 * @Description: ${todo}
 */
@Slf4j
public class DeptReducer extends Reducer<TextPair, Text, Text, Text> {

    private final String textTemp = "%s\t%s";

    @Override
    protected void reduce(TextPair key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        log.info("key: {}", key);
        Iterator<Text> iterator = values.iterator();
        Text v = iterator.next();
        log.info("v: {}", v);
        while (iterator.hasNext()) {
            String text = String.format(textTemp, v.toString(), iterator.next().toString());
            log.info("text: {}", text);
            context.write(
                    new Text(key.getFirst()),
                    new Text(text)
            );
        }
    }
}
